//
//  NavigationTitleView.m
//  MoneyLog
//
//  Created by wicresoft on 5/30/11.
//  Copyright 2011 wicresoft. All rights reserved.
//

#import "NavigationTitleView.h"


@implementation NavigationTitleView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];//allocate titleView
	
	//Create UILable
	UILabel	*titleText = [[UILabel alloc] initWithFrame: CGRectMake(100, 0, 50, 20)];//allocate titleText
	titleText.backgroundColor = [UIColor clearColor];
	[titleText setText:@"Title"];
	[titleView addSubview:titleText];
	[titleText release];//release titleText
	
	//Create Round UIButton
	UIButton *btnNormal = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnNormal setFrame:CGRectMake(0, 0, 40, 20)];
	[btnNormal addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
	[btnNormal setTitle:@"Normal" forState:UIControlStateNormal];
    //btnNormal.font = [UIFont systemFontOfSize:8];
	[titleView addSubview:btnNormal];
	
	//Create Builtin type UIButton
	UIButton *btnBuiltinType = [UIButton buttonWithType:UIButtonTypeContactAdd];
	[btnBuiltinType setFrame:CGRectMake(45, 0, 20, 20)];
	[btnBuiltinType addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
	[titleView addSubview:btnBuiltinType];
	
	//Create Custom image UIButton
	UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnImage setFrame:CGRectMake(70, 0, 20, 20)];
	[btnImage addTarget:self action:@selector(switchCheckState:) forControlEvents:UIControlEventTouchUpInside];
	if([[NSUserDefaults standardUserDefaults] boolForKey:@"isChecked"])
		[btnImage setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
	else
		[btnImage setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
	[titleView addSubview:btnImage];
	
	//Set to titleView
	self.navigationItem.titleView = titleView;
	[titleView release];//release titleView
	
	//Custom backgroundImage UIButton
	UIButton *btnCustom = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btnCustom setFrame:CGRectMake(0, 0, 32, 32)];
	[btnCustom addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
	[btnCustom setTitle:@"CB" forState:UIControlStateNormal];
	[btnCustom setBackgroundImage:[UIImage imageNamed:@"whiteButton.png"] forState:UIControlStateNormal];
	[btnCustom setBackgroundImage:[UIImage imageNamed:@"blueButton.png"] forState:UIControlStateHighlighted];
	//Create UIBarButtonItem with the customed button
	UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnCustom];//allocate rightBarButton
	//Set to rightBarButtonItem
	self.navigationItem.rightBarButtonItem = rightBarButton;
	[rightBarButton release];//release rightBarButton
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
-(IBAction)switchCheckState:(id)sender
{
	if([[NSUserDefaults standardUserDefaults] boolForKey:@"isChecked"])
	{
		[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isChecked"];
		[[[self.navigationItem.titleView subviews] lastObject] setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
	}
	else
	{
		[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isChecked"];
		[[[self.navigationItem.titleView subviews] lastObject] setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
	}
    
}


@end
